package com.barnett.tool;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * TODO<时间处理工具>
 */
public class TimeTool {
	/**
	 * 格式化时间（输出类似于 刚刚, 4分钟前, 一小时前, 昨天这样的时间）
	 * 
	 * @param time
	 *            需要格式化的时间 如"2014-07-14 19:01:45"
	 * @param pattern
	 *            输入参数time的时间格式 如:"yyyy-MM-dd HH:mm:ss"
	 *            <p/>
	 *            如果为空则默认使用"yyyy-MM-dd HH:mm:ss"格式
	 * @return time为null，或者时间格式不匹配，输出空字符""
	 */
	public static String formatDisplayTime(String time, String pattern) {
		String display = "";
		int tMin = 60 * 1000;
		int tHour = 60 * tMin;
		int tDay = 24 * tHour;
		if (BaseTool.isEmpty(pattern)) {
			pattern = "yyyy-MM-dd HH:mm:ss";
		}
		if (time != null) {
			try {
				Date tDate = new SimpleDateFormat(pattern, Locale.getDefault())
						.parse(time);
				Date today = new Date();
				SimpleDateFormat thisYearDf = new SimpleDateFormat("yyyy",
						Locale.getDefault());
				SimpleDateFormat todayDf = new SimpleDateFormat("yyyy-MM-dd",
						Locale.getDefault());
				Date thisYear = new Date(thisYearDf.parse(
						thisYearDf.format(today)).getTime());
				Date yesterday = new Date(todayDf.parse(todayDf.format(today))
						.getTime());
				Date beforeYes = new Date(yesterday.getTime() - tDay);
				if (tDate != null) {
					SimpleDateFormat halfDf = new SimpleDateFormat("MM月dd日",
							Locale.getDefault());
					long dTime = today.getTime() - tDate.getTime();
					if (tDate.before(thisYear)) {
						display = new SimpleDateFormat("yyyy年MM月dd日",
								Locale.getDefault()).format(tDate);
					} else {

						if (dTime < tMin) {
							display = "刚刚";
						} else if (dTime < tHour) {
							display = (int) Math.ceil(dTime / tMin) + "分钟前";
						} else if (dTime < tDay && tDate.after(yesterday)) {
							display = (int) Math.ceil(dTime / tHour) + "小时前";
						} else if (tDate.after(beforeYes)
								&& tDate.before(yesterday)) {
							display = "昨天"
									+ new SimpleDateFormat("HH:mm",
											Locale.getDefault()).format(tDate);
						} else {
							display = halfDf.format(tDate);
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return display;
	}

	/**
	 * 根据一个毫秒数 获取当前一个周的日期毫秒数
	 * 
	 * @param now
	 * @return
	 */
	public static long[] getDates(long now) {
		final long HOUR = 3600 * 1000;
		final long DAY = HOUR * 24;
		Date date = new Date(now);
		long[] dates = new long[7];
		switch (date.getDay()) {
		case 0:
			for (int i = 0; i < 7; i++) {
				dates[i] = now - (7 - i - 1) * DAY;
			}
			return dates;
		default:
			for (int i = 0; i < 7; i++) {
				if (i < date.getDay()) {
					dates[i] = now - (date.getDay() - i - 1) * DAY;
				} else {
					dates[i] = now + (i - date.getDay() + 1) * DAY;
				}
			}
			return dates;
		}
	}

	/**
	 * string 转化为 date
	 * 
	 * @param time
	 * @return
	 */
	public static Date strToDate(String time) {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
				Locale.getDefault());
		try {
			date = sdf.parse(time);
			return date;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}
}
